<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="k" uri="/WEB-INF/k.tld"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>怀才进销存管理软件 - 销售前台</title>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../jslib/date.js"></script>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<link href="../web/css/style.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript">
			
		var calendar;
		$(function(){
			calendar = $('#calendar').calendar({
				fit : true,
				current : new Date(),
				border : false,
				onSelect : function(date) {
					$(this).calendar('moveTo', new Date());
				}
			});

			$('#date').val((new Date()).pattern("yyyy-MM-dd"));

			var rid = $('#saleReturnForm').find('input[name=rid]');
			var ridCombobox = rid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto',
				onLoadSuccess : function() {
					parent.$.messager.progress('close');
				}
			});

			var uid = $('#saleReturnForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto',
				onLoadSuccess : function() {
					parent.$.messager.progress('close');
				}
			});

			$.ajax({
				url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
				data : {sign : 'XSTH'},
				type : "POST",
				success : function(d){
					$('#serial').val(d);
				}
			});

			$("#sserial").live("keypress",function(e) {
				var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
				if (keyCode == 13) {
					var rid = $('#saleReturnForm').find('input[name=rid]').val();
					var sserial = jQuery.trim(jQuery("#sserial").val());
					if(rid==null||rid==""){
						alert("请选择当前仓库！");
						return;
					}else if(sserial==null||sserial==""){
						alert("请输入销售单号！");
						return;
					}else{
						$.ajax({
							url : '${pageContext.request.contextPath}/xsgl/salelistAction!noAuth_checkSalelist.html?model.serial='+sserial+"&model.rid="+rid,
							type : "POST",
							success : function(d){
								var json = $.parseJSON(d);
								if (json.success==null) {
									var f = $('#saleReturnForm');
									f.form("load",{
										slid : json[0].id,
										mname : json[0].mname,
										sdate : json[0].date,
										sserial : json[0].serial,
										uusername : json[0].uusername,
										pointNum : json[0].pointNum
									});
									getSalelistItems(json[0].id);
								}else{
									parent.sy.messagerShow({
										msg : json.msg,
										title : '提示'
									});
								}
							}
						});
					}
				}
			});
		});

		var datagrid;
		function getSalelistItems(id){
			$(function() {
				datagrid = $('#datagrid').datagrid({
					url : 'salelistAction!noAuth_checkItems.html?model.id='+id,
					title : '销售商品列表',
					fit : true,
					fitColumns : false,
					nowrap : false,
					border : false,
					idField : 'id',
					sortName : 'id',
					sortOrder : 'desc',
					checkOnSelect : false,
					selectOnCheck : true,
					frozenColumns : [ [ {
						title : '编号',
						field : 'id',
						width : 150,
						sortable : true
					}] ],
					columns : [ [  {
						title : '商品名称',
						field : 'gname',
						width : 350,
					}  , {
						title : '颜色',
						field : 'gcolor',
						width : 100
					} , {
						title : '尺码',
						field : 'gsize',
						width : 100
					} , {
						title : '售价',
						field : 'salePrice',
						width : 100
					}, {
						title : '销售数',
						field : 'number',
						width : 100
					}, {
						title : '总金额',
						field : 'total',
						width : 100
					}] ],
					onClickRow:function(rowIndex, rowData){
						changReturnGoods();
					} 
				});
			});
	    }


		function changReturnGoods(){
			var rows = datagrid.datagrid('getSelections');
			if (rows.length >0) {
				var salelistId = $(".salelistId").val();
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.ajax({
					url : '${pageContext.request.contextPath}/xsgl/saleReturnlistAction!noAuth_statusReturn.html',
					data : {
						sid:salelistId,
						ids : ids.join(',')
					},
					dataType : 'json',
					success : function(d) {
						$("#returnAmount").val(d.realReturn);
						$("#returnPoint").val(d.returnPoint);
					}
				});
			}else{
				$("#returnAmount").val(0.00);
				$("#returnPoint").val(0);
			}
		}

		function submitSale(){
			var rows = datagrid.datagrid('getRows');
			if (rows.length <=0) {
				alert("请输入正确的销售单号");
			}else{
				rows = datagrid.datagrid('getSelections');
				if (rows.length <=0) {
					alert("请选择退货商品");
				}else{
					var salelistId = $(".salelistId").val();
					var gid = "";
					for ( var i = 0; i < rows.length; i++) {
						gid = rows[i].id+",";
					}
					$(".returnItemIds").val(gid);
					sy.messagerConfirm('请确认', '您确定要退货吗？', function(r) {
						if(r){
							$('#saleReturnForm').form('submit', {
								url : '${pageContext.request.contextPath}/xsgl/saleReturnlistAction!noAuth_add.html',
								success : function(d) {
									var json = $.parseJSON(d);
									sy.messagerShow({
										msg : json.msg,
										title : '提示'
									});
									if(json.success){
										self.location.reload();
									}
								}
							});
							
						}
					});
				}
			}
		}

		
		function logout(){
			sy.messagerConfirm('请确认', '您确定要退出前台销售？', function(r) {
				if(r){
					$.ajax({
						url : '${pageContext.request.contextPath}/loginAction!logout.html',
						type : 'get',
						success:function(){
							self.location.reload();
							if(window.opener){
								opener.location = opener.location;
							}
							window.close();
						}
					});
				}
			});
		}

		</script>
	</head>

	<body>
		<div class="top">
			<div class="logo">
				<img src="${pageContext.request.contextPath}/web/imgs/htlogo.jpg" width="480" height="47" />
			</div>
			<div class="right">
				欢迎您！${sessionInfo.loginName} [${sessionInfo.roleNames}]
				<a href="javascript:void(0)" onclick="logout()">退出登录</a>
				<a href="#">修改密码</a>
			</div>
		</div>
		<div class="content">
			<div class="right">
				<!--日历标题-->
				<div class="title">
					<img src="${pageContext.request.contextPath}/web/imgs/pic1.gif" width="6" height="6" />
					日历
				</div>
				<!--/日历标题-->
				<!--日历-->
				<div style="height:180px;">
					<div id="calendar"></div>
				</div>
				<!--/日历-->
				<!--公告+提醒-->
				<div class="title">
					<ul>
						<li id="cp1" class="cpover" onMouseOver="setTab_('cp',1,5,'cpover')">
							公告<span class="tabRefresh"><a href="javascript:void(0);" onclick="javascript:document.frames('announcementIframe').location.reload()" class="icon-mini-refresh"></a></span>
						</li>
						<li id="cp2" onMouseOver="setTab_('cp',2,5,'cpover')">
							系统提醒<span class="tabRefresh"><a href="javascript:void(0);" onclick="javascript:document.frames('warnIframe').location.reload()" class="icon-mini-refresh"></a></span>
						</li>
					</ul>
				</div>
				<div class="conts">
					<div id="cp_con1">
						<iframe width="100%" scrolling="auto" frameborder="0" onload="javascript:void(0);" name="announcementIframe" id="announcementIframe" src="${pageContext.request.contextPath}/qtmk/announcementAction!noAuth_tabAnnounce.html"></iframe>
					</div>
					<div id="cp_con2" style="display: none;">
						<iframe width="100%" scrolling="no"  frameborder="0" onload="javascript:void(0);" name="warnIframe" id="warnIframe" src="${pageContext.request.contextPath}/qtmk/remindWarnAction!noAuth_tabRemindWarn.html"></iframe>
					</div>
				</div>
				<!--/公告+提醒-->
			</div>
			<div class="left">
				<div class="nav">
					<ul>
						<k:security key="XSGL_QTXS">
						<li>
							<a href="${pageContext.request.contextPath}/xsgl/sale.html">
								<img src="${pageContext.request.contextPath}/web/imgs/navxsgl.jpg" width="104" height="35" /> 
							</a>
						</li>
						</k:security>
						<k:security key="XSGL_XSTH">
						<li>
							<a href="${pageContext.request.contextPath}/xsgl/saleReturn.html">
								<img src="${pageContext.request.contextPath}/web/imgs/nav-xsth.jpg" width="104" height="35" /> 
							</a>
						</li>
						</k:security>
						<k:security key="XSGL_KCCX">
						<li>
							<a href="${pageContext.request.contextPath}/xsgl/amount.html">
								<img src="${pageContext.request.contextPath}/web/imgs/nav-kcgl.jpg" width="104" height="35" /> 
							</a>
						</li>
						</k:security>
						<k:security key="XSGL_HYGL">
						<li>
							<a href="${pageContext.request.contextPath}/xsgl/member.html">
								<img src="${pageContext.request.contextPath}/web/imgs/nav-hygl.jpg" width="104" height="35" /> 
							</a>
						</li>
						</k:security>
						<k:security key="XSGL_RJQS">
						<li>
							<a href="${pageContext.request.contextPath}/xsgl/settlement.html">
								<img src="${pageContext.request.contextPath}/web/imgs/nav-rjqs.jpg" width="104" height="35" /> 
							</a>
						</li>
						</k:security>
						<k:security key="XSGL_XSJLCX">
						<li>
							<a href="${pageContext.request.contextPath}/xsgl/salelist.html">
								<img src="../web/imgs/navxsglcx.jpg" height="35" /> 
							</a>
						</li>
						</k:security>
					</ul>
				</div>
				<div class="cont">
				 <div style=" overflow: hidden;">
					<form id="saleReturnForm">
						<table border="0" cellspacing="4" class="table">
							<input name="slid" type="hidden" class="salelistId"/>
							<input name="items" type="hidden" class="returnItemIds"/>
							<tr>
								<td align="right" valign="top" class="lanse">
									当前仓库：
								</td>
								<td valign="top">
									<input name="rid" type="text" class="input1" id="rid"  />
								</td>
								<td width="90" align="right" class="lanse">
									销售单号：
								</td>
								<td width="130">
									<input name="sserial" type="text" class="input1" id="sserial" value="XS-2013010" />
								</td>
								<td align="right">
									会员名称：
								</td>
								<td>
									<input name="mname" type="text" class="input1" readonly="readonly" id="mname"/>
								</td>
							</tr>
							<tr>
								<td align="right">
									销售日期：
								</td>
								<td>
									<input name="sdate" type="text" class="input1" readonly="readonly"/>
								</td>
								<td width="90" align="right" class="lanse">
									销售营业员：
								</td>
								<td width="130">
									<input name="uusername" type="text" class="input1" readonly="readonly" id="uusername"/>
								</td>
								<td width="72" align="right">
									使用积分：
								</td>
								<td width="130">
									<input name="pointNum" type="text" class="input1" readonly="readonly" id="pointNum" value="0"/>
								</td>
							</tr>
							<tr>
								<td width="72" align="right" class="lanse">
									退货日期：
								</td>
								<td width="130">
									<input name="date" id="date" type="text" onclick="WdatePicker()">
								</td>
								<td align="right" class="lanse">
									退货营业员：
								</td>
								<td>
									<input name="uid" type="text" class="input1" id="uid"  />
								</td>
								<td width="72" align="right" class="lanse">
									退货单号：
								</td>
								<td width="130">
									<input name="serial" type="text" class="input1" readonly="readonly" id="serial" value="0"/>
								</td>
							</tr>
							<tr>
								<td align="right" valign="top">
									备&nbsp;&nbsp;&nbsp;&nbsp;注：
								</td>
								<td colspan="5">
									<label for="textarea"></label>
									<textarea name="remark" id="textarea" cols="38" rows="2"></textarea>
								</td>
							</tr>
						</table>
				    </form>
						
					<div data-options="region:'center',border:false" style="overflow: hidden;height:200px;" class="salelistItems">
						<table id="datagrid"  ></table>
					</div>

					<div class="jiesuan">
						<div class="centjs">
							<table width="100%" border="0" class="jsxx">
								<tr>
									<td>应退：</td>
									<td>
										<input name="returnAmount" type="text" class="input1" id="returnAmount" value="0.00" readonly="readonly" />元
									</td>
								</tr>
								<tr>
									<td>积分：</td>
									<td>
										<input name="returnPoint" type="text" class="input1" id="returnPoint" value="0" readonly="readonly" />点
									</td>
								</tr>
							</table>
						</div>
						<div class="rightjs">
							<ul>
								<li>
									<a href="javascript:void(0);" onclick="submitSale()" class="button2 dis_block">结算打印</a>
								</li>
								<li>
									<a href="javascript:void(0);" onclick="cancelAll()" class="button3 dis_block">取 消</a>
								</li>
							</ul>
						</div>
					</div>
				 </div>
              </div>
			</div>
		</div>
		<div class="foot">
			Copyright @ 2013
			<a style="color:#FFFFFF; text-decoration:none;" target="_blank" href="http://www.miitbeian.gov.cn">浙ICP备13002014号-1</a>
			<br>
			杭州怀才商贸有限公司 
		</div>
		<div id="bg" class="bg" style="display: none;"></div>
		<iframe id='popIframe' class='popIframe' frameborder='0'></iframe>
	</body>
</html>
